﻿/// ****************************************************************************
/// Author:	        Robin Zhu
/// Create Date:	2012-5-6
/// Purpose: 		权限审核服务的WCF实现版本的客户端类
/// ****************************************************************************
/// Modify By		Date			Remark
/// Robin           2015-1-12       实现异步的调用
/// Robin           2015-2-26       增加GetRolesByUrl方法
/// ****************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace RB.Web.Security.Wcf
{
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName = "RB.Web.Security.IAuthorizeService")]
    public interface IAuthorizeServiceClient : IAuthorizeService
    {
        [System.ServiceModel.OperationContractAttribute(IsOneWay = true, AsyncPattern = true, Action = "http://tempuri.org/IAuthorizeService/LogAction")]
        System.IAsyncResult BeginLogAction(string appCode, string ticket, string permCode, string pathAndQuery, bool authorized, string ipAddress, string httpForwarded, System.AsyncCallback callback, object asyncState);

        void EndLogAction(System.IAsyncResult result);
    }

    /// <summary>
    /// 权限审核服务的WCF实现版本的客户端类
    /// </summary>
    public class WcfAuthorizeService : ClientBase<IAuthorizeServiceClient>, IAuthorizeServiceClient
    {
        private BeginOperationDelegate onBeginLogActionDelegate;

        private EndOperationDelegate onEndLogActionDelegate;

        private System.Threading.SendOrPostCallback onLogActionCompletedDelegate;

        public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> LogActionCompleted;

        public WcfAuthorizeService()
            : base(new ServiceEndpoint(ContractDescription.GetContract(typeof(IAuthorizeServiceClient)),
                new BasicHttpBinding() { MaxReceivedMessageSize = 655360 },
                new EndpointAddress(WcfSecurityConfigSection.Current.GetServiceAddress("AuthorizeService.svc"))))
        {
        }

        public bool IsAllowed(string appCode, Guid userId, string path, string httpMethod)
        {
            return base.Channel.IsAllowed(appCode, userId, path, httpMethod);
        }

        public bool Authorize(string appCode, string ticket, string pathAndQuery, string httpMethod)
        {
            return base.Channel.Authorize(appCode, ticket, pathAndQuery, httpMethod);
        }


        public AppMenu[] GetAppMenus(string appCode, Guid userId)
        {
            return base.Channel.GetAppMenus(appCode, userId);
        }

        public string[] GetPermissionCodesByTicket(string appCode, string ticket)
        {
            return base.Channel.GetPermissionCodesByTicket(appCode, ticket);
        }

        public Permission[] GetAllPermissions(string appCode)
        {
            return base.Channel.GetAllPermissions(appCode);
        }


        public void LogAction(string appCode, string ticket, string permCode, string pathAndQuery, bool authorized, string ipAddress, string httpForwarded)
        {
            LogActionAsync(appCode, ticket, permCode, pathAndQuery, authorized, ipAddress, httpForwarded);
        }

        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
        public System.IAsyncResult BeginLogAction(string appCode, string ticket, string permCode, string pathAndQuery, bool authorized, string ipAddress, string httpForwarded, System.AsyncCallback callback, object asyncState)
        {
            return base.Channel.BeginLogAction(appCode, ticket, permCode, pathAndQuery, authorized, ipAddress, httpForwarded, callback, asyncState);
        }

        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
        public void EndLogAction(System.IAsyncResult result)
        {
            base.Channel.EndLogAction(result);
        }

        private System.IAsyncResult OnBeginLogAction(object[] inValues, System.AsyncCallback callback, object asyncState)
        {
            string appCode = ((string)(inValues[0]));
            string ticket = ((string)(inValues[1]));
            string permCode = ((string)(inValues[2]));
            string pathAndQuery = ((string)(inValues[3]));
            bool authorized = ((bool)(inValues[4]));
            string ipAddress = ((string)(inValues[5]));
            string httpForwarded = ((string)(inValues[6]));
            return this.BeginLogAction(appCode, ticket, permCode, pathAndQuery, authorized, ipAddress, httpForwarded, callback, asyncState);
        }

        private object[] OnEndLogAction(System.IAsyncResult result)
        {
            this.EndLogAction(result);
            return null;
        }

        private void OnLogActionCompleted(object state)
        {
            if ((this.LogActionCompleted != null))
            {
                InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
                this.LogActionCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
            }
        }

        public void LogActionAsync(string appCode, string ticket, string permCode, string pathAndQuery, bool authorized, string ipAddress, string httpForwarded)
        {
            this.LogActionAsync(appCode, ticket, permCode, pathAndQuery, authorized, ipAddress, httpForwarded, null);
        }

        public void LogActionAsync(string appCode, string ticket, string permCode, string pathAndQuery, bool authorized, string ipAddress, string httpForwarded, object userState)
        {
            if ((this.onBeginLogActionDelegate == null))
            {
                this.onBeginLogActionDelegate = new BeginOperationDelegate(this.OnBeginLogAction);
            }
            if ((this.onEndLogActionDelegate == null))
            {
                this.onEndLogActionDelegate = new EndOperationDelegate(this.OnEndLogAction);
            }
            if ((this.onLogActionCompletedDelegate == null))
            {
                this.onLogActionCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnLogActionCompleted);
            }
            base.InvokeAsync(this.onBeginLogActionDelegate,
                new object[] {
                        appCode,
                        ticket,
                        permCode,
                        pathAndQuery,
                        authorized,
                        ipAddress,
                        httpForwarded
            },
            this.onEndLogActionDelegate, this.onLogActionCompletedDelegate, userState);
        }


        public string[] GetRolesByUrl(string appCode, string url)
        {
            return base.Channel.GetRolesByUrl(appCode, url);
        }


        public AppEntry[] GetAppEntries(string ticket)
        {
            return base.Channel.GetAppEntries(ticket);
        }
    }
}
